<template>
  <div>
    <div>
      <div class="ccui-navigation">
        <el-button
          size="mini"
          type="primary-icon-medium"
          icon="icon-ccui-return"
          @click="handleBack"
          >返回</el-button
        >
        <div class="ccui-navigation-divider" />
        <!-- 面包屑 -->
        <el-breadcrumb
          class="ccui-navigation-crumbs-container"
          separator-class="el-icon-arrow-right"
        >
          <el-breadcrumb-item :to="{ path: '/contractManagement' }"
            >供应链管理</el-breadcrumb-item
          >
          <el-breadcrumb-item :to="{ path: '/checkmanagement/manage' }"
            >验收管理</el-breadcrumb-item
          >
          <!-- <el-breadcrumb-item
            :to="{
              path: '/checkmanagement/manage/arrival-check-list'
            }"
            >验收管理</el-breadcrumb-item
          > -->
          <el-breadcrumb-item :to="{ path: '/checkmanagement/returnsManage' }"
            >退货管理</el-breadcrumb-item
          >
          <el-breadcrumb-item>
            <span
              style="color: #4a4a4a"
              v-if="
                !$route.query.bowOutCertBO || !$route.query.bowOutBillBOList
              "
              >新增</span
            >
            <span v-else>
              <span>编辑</span>
              <span style="margin-left: 10px; color: #4a4a4a">{{
                $route.query.bowOutCertBO.bowOutName
              }}</span>
            </span>
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <div class="ccui-app-container-detail">
        <div class="ccui-app-container-detail-body">
          <div
            :class="focus == 0 ? 'ccui-top-title' : 'top-title'"
            @click="tabChange(0)"
          >
            <span style="cursor: pointer">基本信息</span>
          </div>
          <div
            :class="focus == 1 ? 'ccui-top-title' : 'top-title'"
            style="margin-left: 20px"
            @click="tabChange(1)"
          >
            <span style="cursor: pointer">采购清单</span>
          </div>
          <div
            :class="focus == 2 ? 'ccui-top-title' : 'top-title'"
            style="margin-left: 20px"
            @click="tabChange(2)"
          >
            <span style="cursor: pointer">附件信息</span>
          </div>
          <!-- 基本信息 -->
          <el-form
            v-show="focus == 0"
            label-width="150px"
            class="demo-ruleForm"
            style="margin-top: 20px"
            :model="formData"
            :rules="rules"
            ref="formData"
          >
            <el-row type="flex" justify="centert" :gutter="0">
              <el-col :span="12">
                <el-form-item label="填报单位：" prop="reportInOrgName">
                  <el-select
                    v-model="formData.reportInOrgName"
                    placeholder="请选择填报单位"
                    @change="changeReportInOrgId"
                    style="width:100%;"
                    :disabled="
                      !$route.query.bowOutCertBO ||
                      !$route.query.bowOutBillBOList
                        ? false
                        : true
                    "
                  >
                    <el-option
                      v-for="item in reportOptions"
                      :key="item.orgId"
                      :label="item.name"
                      :value="item.name"
                    >
                    </el-option>
                  </el-select>
                  <!-- <el-input
                    v-model="formData.reportInOrgName"
                    clearable
                    disabled
                  ></el-input> -->
                </el-form-item>
              </el-col>

              <el-col :span="12" :offset="4">
                <el-form-item label="供应商：">
                  <el-input v-model="formData.supplier" disabled></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex" justify="center" :gutter="0">
              <el-col :span="12">
                <el-form-item label="退货单编号：" required>
                  <el-input
                    v-model="formData.bowOutNumber"
                    clearable
                    disabled
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="4">
                <el-form-item label="退货单名称：" prop="bowOutName">
                  <el-input
                    v-model="formData.bowOutName"
                    placeholder="请输入退货单名称"
                    @blur="handleTrim('bowOutName')"
                    clearable
                    maxlength="300"
                    show-word-limit
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex" justify="center" :gutter="0">
              <el-col :span="12" class="relative">
                <el-form-item label="验收单编号：" prop="acceptanceNumber">
                  <el-input
                    v-model="formData.acceptanceNumber"
                    placeholder="请选择验收单编号"
                    disabled
                  ></el-input>
                  <el-button
                    plain
                    type="single-number"
                    size="mini"
                    class="chereay"
                    @click="choose"
                    v-if="
                      !$route.query.bowOutCertBO ||
                        !$route.query.bowOutBillBOList
                    "
                    >选择</el-button
                  >
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="4">
                <el-form-item label="合同编号：" prop="contractNumber">
                  <el-input
                    v-model="formData.contractNumber"
                    clearable
                    disabled
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex" justify="center" :gutter="2">
              <el-col :span="12">
                <el-form-item label="验收人：" prop="acceptor">
                  <el-input
                    v-model="formData.acceptor"
                    clearable
                    disabled
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="4">
                <el-form-item label="验收人联系电话：" prop="acceptancePhone">
                  <el-input
                    v-model="formData.acceptancePhone"
                    maxlength="11"
                    show-word-limit
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex" justify="center" :gutter="2">
              <el-col :span="12">
                <el-form-item label="退货人：" prop="bowOutPeople">
                  <el-input
                    v-model="formData.bowOutPeople"
                    placeholder="请输入"
                    maxlength="50"
                    show-word-limit
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="12" :offset="4">
                <el-form-item label="退货人联系电话：" prop="bowOutPhone">
                  <el-input
                    v-model="formData.bowOutPhone"
                    placeholder="请输入"
                    maxlength="11"
                    show-word-limit
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row type="flex" justify="center" :gutter="2">
              <el-col :span="12">
                <el-form-item label="供应商联系电话：" prop="supplierPhone">
                  <el-input
                    v-model="formData.supplierPhone"
                    placeholder="请输入"
                    maxlength="11"
                    show-word-limit
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" :offset="4">
                <el-form-item label="退货日期：">
                  <el-date-picker
                    type="date"
                    placeholder="请选择"
                    v-model="formData.bowOutDate"
                    style="width: 100%"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex" justify="center" :gutter="0">
              <el-col :span="12">
                <el-form-item label="合同名称：" prop="contractName">
                  <el-input
                    v-model="formData.contractName"
                    clearable
                    disabled
                  ></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="12" :offset="4">
                <el-form-item label="编制日期：" prop="compileDate">
                  <el-date-picker
                    v-model="formData.compileDate"
                    type="date"
                    style="width: 100%"
                    placeholder="请选择"
                    disabled
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex" justify="flex-start" :gutter="0">
              <el-col :span="12">
                <el-form-item label="编制人：">
                  <el-input
                    v-model="formData.compilePeople"
                    placeholder="请输入"
                    disabled
                    maxlength="50"
                    show-word-limit
                    style="width: 77%"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex" justify="flex-start" :gutter="0">
              <el-col :span="12">
                <el-form-item label="退货原因：" prop="bowOutType">
                  <el-select
                    v-model="formData.bowOutType"
                    placeholder="请选择"
                    @change="getSelect"
                    style="width: 77%"
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row
              type="flex"
              justify="flex-start"
              :gutter="0"
              style="margin-top: -10px"
            >
              <el-col :span="24">
                <el-form-item prop="bowOutReason" label="退货备注：">
                  <el-input
                    type="textarea"
                    v-model="formData.bowOutReason"
                    placeholder="请输入"
                    style="margin-top: 10px; border-radius: 4px"
                    clearable
                    maxlength="1000"
                    show-word-limit
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <!-- 采购清单 -->
          <!-- <keep-alive> -->
          <div
            class="ccui-multifunctional-table-wrapper"
            style="marign-top: 10px"
            v-show="focus == 1"
          >
            <div
              style="
                width: 100%;
                height: 50px;
                backgroung: red;
                display: flex;
                justify-content: flex-end;
              "
            >
              <!-- <el-button
                size="mini"
                plain
                type="single-number"
                @click="deleteRow"
                >删除行</el-button
              > -->
              <el-button
                size="mini"
                plain
                type="single-number"
                @click="batchRow"
                >批量设置退货地点</el-button
              >
            </div>
            <ux-grid
              align="center"
              ref="multipleTable"
              :data="purchaseData"
              border
              class="ccui-multifunctional-table"
              :summary-method="getSummaries"
              lazy
              show-summary
              keep-source
              max-height="9999"
              show-overflow
              @selection-change="selectionLineChangeHandle"
              :header-cell-class-name="must"
              :highlightCurrentRow="false"
              :widthResize="true"
              :edit-config="{ trigger: 'click', mode: 'cell' }"
            >
              <ux-table-column type="checkbox" width="55" fixed="left" />
              <ux-table-column
                type="index"
                title="序号"
                width="80"
                fixed="left"
              />
              <ux-table-column
                title="设备物资编码"
                field="materialCode"
                width="110"
                fixed="left"
                show-overflow-tooltip
              />
              <ux-table-column
                title="设备物资名称"
                field="materialName"
                fixed="left"
                width="300"
                show-overflow-tooltip
              />
              <ux-table-column
                title="设备物资说明"
                field="materialState"
                width="130"
                show-overflow-tooltip
              />
              <ux-table-column
                title="分类名称"
                field="categoryName"
                width="130"
                show-overflow-tooltip
              />
              <ux-table-column
                title="计量单位"
                field="measureUnit"
                width="90"
                show-overflow-tooltip
              />
              <ux-table-column
                title="退货地点"
                field="testdaohuo"
                width="300"
                show-overflow-tooltip
                :edit-render="{ autofocus: '.el-input__inner' }"
              >
                <template v-slot:edit="scope">
                  <el-input
                    v-model="scope.row.testdaohuo"
                    placeholder="请选择地点"
                    readonly
                    @focus="returnSite(scope.row)"
                  ></el-input>
                </template>
                <template v-slot="scope">
                  <span class="my-input-sc span-input">{{
                    scope.row.testdaohuo
                  }}</span>
                </template>
              </ux-table-column>
              <ux-table-column
                title="验收数量"
                field="acceptanceCurrentNumber"
                width="100"
                header-align="left"
              />

              <!-- 编辑绑定 -->
              <ux-table-column
                title="实际验收数量"
                field="acceptanceEndNumber"
                width="150"
                header-align="left"
                v-if="
                  $route.query.bowOutCertBO || $route.query.bowOutBillBOList
                "
              >
              </ux-table-column>

              <!-- 新增绑定 -->
              <ux-table-column
                title="实际验收数量"
                field="acceptanceCurrentEndNumber"
                width="150"
                header-align="left"
                v-else
              >
              </ux-table-column>

              <ux-table-column
                title="本次退货数量"
                field="bowOutNumber"
                width="130"
                :edit-render="{ autofocus: '.el-input__inner' }"
              >
                <template v-slot:edit="{ row }">
                  <el-input
                    placeholder="请输入本次退货数量"
                    v-model="row.bowOutNumber"
                    @blur="validatorNum($event, row)"
                  >
                  </el-input>
                </template>
                <template v-slot="scope">
                  <span class="my-input-sc">{{ scope.row.bowOutNumber }}</span>
                </template>
              </ux-table-column>

              <ux-table-column
                width="130"
                title="生产厂家/品牌"
                field="manufacturerBrand"
                show-overflow-tooltip
              />
              <ux-table-column
                width="200"
                title="备注"
                field="remake"
                show-overflow-tooltip
              ></ux-table-column>
            </ux-grid>
          </div>

          <!-- 附件信息 -->
          <attachment
            ref="attachment"
            @fileIdArr="fileIdArr"
            :fileArrs="fileArrs"
            v-show="focus == 2"
            v-if="isTrue"
          />

          <!-- 底部保存 -->
          <el-bottom-guide>
            <ul class="ccui-bottom-button">
              <li>
                <el-button
                  type="bottom"
                  icon="icon-ccui-save"
                  @click="submitHandle(0)"
                  >保存并校验</el-button
                >
              </li>
              <li>
                <el-button
                  type="bottom"
                  icon="icon-ccui-export"
                  @click="submitHandle(1)"
                  >保存并提交</el-button
                >
              </li>
              <li>
                <el-button
                  type="bottom"
                  icon="icon-ccui-return"
                  @click="handleBack"
                  >返回</el-button
                >
              </li>
            </ul>
          </el-bottom-guide>

          <!-- 验收单编号选择弹框 -->
          <chooseSupplier
            ref="brachFormVisible"
            @partyBName="partyBName"
            :partBdata="partBdata"
            :reportInOrgId="formData.reportInOrgId"
          />
          <listSettings
            ref="listSettings"
            dialogTitle="退货地点"
            :isSelect="true"
            status="address"
            addressLabel="退货地点"
            :detail="rowSite"
            @ok="getSite"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import validators from "@/basa/utils/validators";
import {
  saveBowOutCert,
  getAcceptanceCert,
  editBowOutCert
} from "../api/bowOut";
import attachment from "../components/uploadFiles";
import chooseSupplier from "../components/chooseSupplier";
import { getUserInfo, getReportInOrgData } from "@/basa/api";

import { addUrlPerFix, formatNumber } from "@/basa/utils";
import listSettings from "@/basa/components/public/listSettings";
export default {
  data() {
    var Mobile = (rule, value, callback) => {
      var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (value) {
        if (value.match(myreg)) {
          callback();
        } else {
          this.$showErrorMessage("请输入正确的手机号");
          callback(new Error(rule.message || "请输入正确的手机号"));
        }
      } else {
        callback();
      }
    };
    return {
      isTrue: false,
      checkId: "",
      rowSite: {},
      reportOptions: [],
      formData: {
        reportInOrgName: "", //填报单位
        supplier: "", //供应商
        bowOutNumber: "", //退货单编号
        bowOutName: "", //退货单名称
        acceptanceNumber: "", //验收单编号
        supplierPhone: "", //供应商联系电话
        // bowOutPlace: "", //退货地点
        acceptor: "", //验收人
        acceptancePhone: "", //验收人联系电话
        bowOutPeople: "", //退货人
        bowOutPhone: "", //退货人联系电话
        bowOutDate: "", //退货日期
        contractNumber: "", //合同编号
        contractName: "", //合同名称
        compilePeople: "", //编制人
        compileDate: "", //编制日期
        bowOutType: "", //退货类型
        bowOutReason: "", //退货原因
        reportInOrgId: 0
      },
      searchPop: {
        acceptanceNumber: "", //验收单编号
        acceptanceName: "", //验收单名称
        supplier: "", //供应商
        reportInOrgName: "" //填报单位
      },
      options: [
        {
          value: "质量问题",
          label: "质量问题"
        },
        {
          value: "其他",
          label: "其他"
        }
      ],
      rules: {
        acceptancePhone: [
          {
            validator: Mobile,
            trigger: "blur"
          }
        ],
        bowOutPhone: [
          {
            validator: Mobile,
            trigger: "blur"
          }
        ],
        supplierPhone: [
          {
            validator: Mobile,
            trigger: "blur"
          }
        ],
        bowOutNumber: [
          {
            required: true,
            message: "请输入退货单编号",
            trigger: "blur"
          },
          {
            validator: validators.numAndLetter,
            message: "只能输入数字和字母",
            trigger: "blur"
          }
        ],
        bowOutName: [
          {
            required: true,
            message: "请输入退货单名称",
            trigger: "blur"
          }
        ],
        acceptanceNumber: [
          {
            required: true,
            message: "请选择验收单编号",
            trigger: "blur"
          },
          {
            validator: validators.numAndLetter,
            message: "只能输入数字和字母",
            trigger: "blur"
          }
        ],
        // bowOutPlace: [
        //   {
        //     required: true,
        //     message: "请输入退货地点",
        //     trigger: "blur"
        //   }
        // ],
        bowOutType: [
          {
            required: true,
            message: "请选择退货类型",
            trigger: "blur"
          }
        ],
        bowOutReason: [
          {
            required: true,
            message: "请输入退货备注",
            trigger: "blur"
          }
        ],
        compileDate: [
          {
            required: true,
            message: "请选择编制日期",
            trigger: "blur"
          }
        ],
        bowOutPeople: [
          { required: true, message: "请输入退货人", trigger: "blur" }
        ],
        reportInOrgName: [
          { required: true, message: "填报单位未填写", trigger: "blur" }
        ],
        contractNumber: [
          { required: true, message: "合同编号未填写", trigger: "blur" }
        ],
        contractName: [
          { required: true, message: "合同名称未填写", trigger: "blur" }
        ]
      },
      partBdata: [], //乙方数据
      selectVal: [],
      purchaseData: [], //采购清单list
      isShowSearch: true,
      isEdit: false,
      dialogVisible: false,
      focus: 0,
      editBowOutNumber: "", //编辑退货数量
      bowOut: "", //退货类型
      reportInOrgId: "", //填报单位编码
      reportOrgDeptId: "", //填报单位部门编码
      reportOrgDeptName: "", //填报单位部门
      supplierCode: "", //供应商编码
      contractId: "", //合同主键
      acceptanceId: "", //验收单主键
      uploadIds: [], //上传的文件id数组
      commitType: "", //提交状态
      invoiceId: "",
      invoiceNumber: "",
      invoiceName: "",
      orderId: "",
      orderNumber: "",
      orderName: "",
      categoryName: "",
      supplierOrgId: "", //供应商主键
      fileArrs: [],
      bowOutText: "",
      bowText: [],
      allBowoutNum: "",
      reportInOrgObj: {
        title: "填报单位模糊查询",
        tableData: [],
        recordsTotal: 0,
        formDate: {
          mainType: "A",
          orgid: "",
          pageNo: 1,
          pageSize: 20
        }
      }
    };
  },
  components: {
    attachment,
    chooseSupplier,
    listSettings
  },
  watch: {
    "formData.reportInOrgName": {
      handler(n) {
        if (n) {
          const tempObj = this.reportOptions.find(e => e.name === n);
          if (tempObj) {
            this.formData.reportInOrgId = tempObj.orgId;
            this.formData.reportInOrgName = tempObj.name;
          }
        }
      },
      immediate: true,
      deep: true
    }
  },
  activated() {
    if (this.$route.query.bowOutCertBO || this.$route.query.bowOutBillBOList) {
      let date = this.$route.query.bowOutCertBO.compileDate;
      this.formData = {
        reportInOrgName: this.$route.query.bowOutCertBO.reportInOrgName, //填报单位
        reportInOrgId: Number(this.$route.query.bowOutCertBO.reportInOrgId),
        supplier: this.$route.query.bowOutCertBO.supplier, //供应商
        bowOutNumber: this.$route.query.bowOutCertBO.bowOutNumber, //退货单编号
        bowOutName: this.$route.query.bowOutCertBO.bowOutName, //退货单名称
        acceptanceNumber: this.$route.query.bowOutCertBO.acceptanceNumber, //验收单编号
        acceptor: this.$route.query.bowOutCertBO.acceptor, //验收人
        bowOutPeople: this.$route.query.bowOutCertBO.bowOutPeople, //退货人
        bowOutDate: this.$route.query.bowOutCertBO.bowOutDate, //退货日期
        bowOutPhone: this.$route.query.bowOutCertBO.bowOutPhone, //退货人联系电话
        supplierPhone: this.$route.query.bowOutCertBO.supplierPhone, //供应商联系电话
        // bowOutPlace: this.$route.query.bowOutCertBO.bowOutPlace, //退货地点
        acceptancePhone: this.$route.query.bowOutCertBO.acceptancePhone, //验收人联系电话
        contractNumber: this.$route.query.bowOutCertBO.contractNumber, //合同编号
        contractName: this.$route.query.bowOutCertBO.contractName, //合同名称
        compilePeople: this.$route.query.bowOutCertBO.compilePeople, //编制人
        compileDate: date.replace(/-/g, "/"), //编制日期
        bowOutType: this.$route.query.bowOutCertBO.bowOutType, //退货类型bowOutTypeCod
        bowOutReason: this.$route.query.bowOutCertBO.bowOutReason //退货原因
      };
      this.purchaseData = this.$route.query.bowOutBillBOList;
      this.purchaseData.forEach(e => {
        e.acceptanceCurrentNumber = formatNumber(e.acceptanceCurrentNumber, 3);
        e.bowOutNumber = formatNumber(e.bowOutNumber, 3);
        e.acceptanceEndNumber = formatNumber(e.acceptanceEndNumber, 3);
      });
      this.bowText = this.$route.query.bowOutBillBOList;
      this.bowText.forEach(e => {
        e.newBowNumber = e.bowOutNumber;
      });
      if (this.purchaseData) {
        this.purchaseData.forEach((e, i) => {
          if (i == 0) {
            this.categoryName = e.categoryName;
          }
          e.testdaohuo =
            e.areaName +
            e.provinceName +
            e.cityName +
            e.countyName +
            e.acceptancePlace;
        });
      }
      this.bowOut = this.$route.query.bowOutCertBO.bowOutTypeCode;
      this.bowOutText = this.$route.query.bowOutCertBO.bowOutType;

      this.fileArrs = this.$route.query.bowOutFiles;
      this.fileArrs.forEach((e, i) => {
        e.fileSize = Math.round(e.fileSize / 1024) + "kb";
      });
      this.isTrue = true;
    } else {
      this.isTrue = true;
      this.purchaseData = [];
      this.fileArrs = [];
      this.uploadIds = [];
      this.focus = 0;
      this.formData = {
        reportInOrgId: 0,
        reportInOrgName: "", //填报单位
        supplier: "", //供应商
        bowOutNumber: "", //退货单编号
        bowOutName: "", //退货单名称
        acceptanceNumber: "", //验收单编号
        supplierPhone: "", //供应商联系电话
        // bowOutPlace: "", //退货地点
        acceptor: "", //验收人
        acceptancePhone: "", //验收人联系电话
        bowOutPeople: "", //退货人
        bowOutPhone: "", //退货人联系电话
        bowOutDate: "", //退货日期
        contractNumber: "", //合同编号
        contractName: "", //合同名称
        compilePeople: this.getUserInfo(), //编制人
        compileDate: this.$set(this.formData, "compileDate", new Date()), //编制日期
        bowOutType: "", //退货类型
        bowOutReason: "" //退货原因
      };
      console.log(this.fileArrs);
      this.getUserInfo();
    }
    this.focus = 0;
  },
  methods: {
    handleTrim(key) {
      this.$set(
        this.formData,
        key,
        this.formData[key].replace(/(^\s*)|(\s*)$/g, "")
      );
    },
    partyBName(val) {
      let tabId = val.id;
      //   this.reportInOrgId = val.reportInOrgId; //填报单位编码
      this.reportOrgDeptId = val.reportOrgDeptId; //填报单位部门编码
      this.reportOrgDeptName = val.reportOrgDeptName; //填报单位部门
      this.supplierCode = val.supplierCode; //供应商编码
      this.contractId = val.contractId; //合同主键
      this.acceptanceId = val.id; //验收单主键
      this.compilePeople = val.compilePeople; //编制人
      this.compileDate = val.compileDate; //编制日期
      this.invoiceId = val.invoiceId;
      this.invoiceNumber = val.invoiceNumber;
      this.invoiceName = val.invoiceName;
      this.orderId = val.orderId;
      this.orderNumber = val.orderNumber;
      this.orderName = val.orderName;
      let baseInfo = {};
      getAcceptanceCert({ id: tabId }).then(res => {
        let arr = res.data.data.acceptanceBillBOList;

        arr.forEach(e => {
          e.acceptanceBillId = e.id;
          e.acceptanceEndNumber = e.currentAcceptanceEndNumber;
          this.$set(e, "bowOutNumber", 0);
          this.$set(
            e,
            "testdaohuo",
            e.areaName +
              e.provinceName +
              e.cityName +
              e.countyName +
              e.acceptancePlace
          );

          e.acceptanceCurrentNumber = formatNumber(
            e.acceptanceCurrentNumber,
            3
          );
          e.acceptanceCurrentEndNumber = formatNumber(
            e.acceptanceCurrentEndNumber,
            3
          );
          e.bowOutNumber = formatNumber(e.bowOutNumber, 3);
        });
        this.purchaseData = arr;
        if (this.purchaseData) {
          this.purchaseData.forEach((e, i) => {
            if (i == 0) {
              this.categoryName = e.categoryName;
            }
          });
        }

        baseInfo = res.data.data.acceptanceCertBO;
        let date = baseInfo.compileDate;
        this.supplierOrgId = baseInfo.supplierOrgId;
        this.formData = {
          ...this.formData,
          supplierPhone: baseInfo.supplierPhone, //供应商联系电话
          acceptanceNumber: baseInfo.acceptanceNumber, //验收单编号
          supplier: baseInfo.supplier, //供应商
          reportInOrgName: baseInfo.reportInOrgName, //填报单位
          acceptanceId: baseInfo.id,
          //   bowOutPlace: baseInfo.arrivalPlace, //退货地点
          acceptor: baseInfo.acceptor, //验收人
          acceptancePhone: baseInfo.acceptancePhone, //验收人联系电话
          contractName: baseInfo.contractName, //合同名称
          contractNumber: baseInfo.contractNumber, //合同名称
          compilePeople: baseInfo.compilePeople, //编制人
          compileDate: date.replace(/-/g, "/") //编制日期
        };
        this.dialogVisible = false;
      });
    },
    handleBack() {
      this.$router.push("/checkManagement/returnsManage/salesRegister-list");
      this.formData = {};
      this.isTrue = false;
    },
    // 多选框的值
    getSelect(value) {
      this.bowOutText = value;
      if (value == "质量问题") {
        this.bowOut = 1;
      } else if (value == "其他") {
        this.bowOut = 2;
      }
    },
    tabChange(index) {
      switch (index) {
        case 0:
          this.focus = 0;
          break;
        case 1:
          this.focus = 1;
          break;
        case 2:
          this.focus = 2;
          break;
      }
    },
    // 输入退货数量
    validatorNum(e, row) {
      if (isNaN(e.target.value * 1)) {
        // row.bowOutNumber = 0;
        row.bowOutNumber = formatNumber(0, 3);
      } else {
        if (e.target.value * 1 < 0) {
          // row.bowOutNumber = 0;
          row.bowOutNumber = formatNumber(0, 3);
        } else {
          row.bowOutNumber = formatNumber(e.target.value, 3);
        }
      }
    },
    // 选择验收单编号
    choose() {
      if (this.formData.reportInOrgId) {
        this.$refs.brachFormVisible.dialogTableVisibletextarea = true;
        this.$refs.brachFormVisible.listConSupPage();
      } else {
        this.$showErrorMessage("请选择填报单位");
      }
    },
    // 采购清单table获取选中的数据
    selectionLineChangeHandle(val) {
      this.selectVal = val;
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (
          [
            "bowOutNumber",
            "acceptanceCurrentNumber",
            "acceptanceEndNumber"
          ].includes(column.property)
        ) {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                let num = Number(prev) + Number(curr);
                return num;
              } else {
                return Number(prev);
              }
            }, 0);
            if (index == 8 || index == 10 || index == 9) {
              sums[index] = formatNumber(sums[index], 3);
            }
            if (index == 10) {
              this.allBowoutNum = sums[index];
            }
          }
        }
      });
      return [sums];
    },
    handleClose() {
      this.dialogVisible = false;
    },
    fileIdArr(value) {
      this.uploadIds = value;
    },
    getRules(value) {
      return new Promise((resolve, reject) => {
        var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        if (value) {
          if (value.match(myreg)) {
            resolve();
          } else {
            reject("请输入正确的手机号");
          }
        } else {
          resolve();
        }
      });
    },
    formatDateTime(date) {
      let _time = new Date(date);
      var y = _time.getFullYear();
      var m = _time.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      var d = _time.getDate();
      d = d < 10 ? "0" + d : d;
      var h = _time.getHours();
      h = h < 10 ? "0" + h : h;
      var minute = _time.getMinutes();
      var seconds = _time.getSeconds();
      minute = minute < 10 ? "0" + minute : minute;
      seconds = seconds < 10 ? "0" + seconds : seconds;
      return y + "/" + m + "/" + d + " " + h + ":" + minute + ":" + seconds;
    },
    getBaseMessage() {
      console.log(this.$refs);
      return new Promise((resolve, reject) => {
        this.$refs["formData"].validate(valid => {
          if (valid) {
            resolve(this.formData);
          } else {
            reject(0);
            return false;
          }
        });
      });
    },
    // 保存的接口公共部分
    async publiceMessage() {
      try {
        await this.getBaseMessage();
        await this.getRules(this.formData.acceptancePhone);
        await this.getRules(this.formData.supplierPhone);
        await this.getRules(this.formData.bowOutPhone);
        let message = {
          businessType: "1",
          reportInOrgName: this.formData.reportInOrgName, //填报单位
          reportInOrgId: Number(this.formData.reportInOrgId),
          supplier: this.formData.supplier, //供应商
          bowOutName: this.formData.bowOutName, //退货单名称
          acceptanceNumber: this.formData.acceptanceNumber, //验收单编号
          acceptancePhone: this.formData.acceptancePhone, //验收人联系电话
          supplierPhone: this.formData.supplierPhone, //供应商联系电话
          contractNumber: this.formData.contractNumber, //合同编号
          contractName: this.formData.contractName, //合同名称
          compilePeople: this.formData.compilePeople, //编制人
          compileDate: Date.parse(
            this.formatDateTime(this.formData.compileDate)
          ), //编制日期
          bowOutTypeCode: this.bowOut, //退货类型
          bowOutType: this.bowOutText,
          bowOutReason: this.formData.bowOutReason, //退货原因
          //   reportInOrgId: this.reportInOrgId, //填报单位编码
          reportOrgDeptId: this.reportOrgDeptId, //填报单位部门编码
          reportOrgDeptName: this.reportOrgDeptName, //填报单位部门
          supplierCode: this.supplierCode, //供应商编码
          contractId: this.contractId, //合同主键
          acceptanceId: this.acceptanceId, // 验收单主键
          submitStatus: this.commitType, //提交状态:0未提交，1已提交
          submitTime: Date.parse(new Date()), //提交时间,
          bowOutNumber: this.editBowOutNumber, //退货数量
          invoiceId: this.invoiceId,
          invoiceNumber: this.invoiceNumber,
          invoiceName: this.invoiceName,
          orderId: this.orderId,
          orderNumber: this.orderNumber,
          orderName: this.orderName,
          categoryName: this.categoryName,
          supplierOrgId: this.supplierOrgId, //供应商主键
          acceptor: this.formData.acceptor, //验收人
          bowOutPeople: this.formData.bowOutPeople, //退货人
          bowOutPhone: this.formData.bowOutPhone, //退货人联系电话
          bowOutDate: Date.parse(this.formatDateTime(this.formData.compileDate)) //退货日期
        };
        if (this.uploadIds.length <= 0) {
          this.$showErrorMessage("请上传附件");
          this.focus = 2;
          return;
        }
        this.purchaseData.forEach(e => {
          if (e.testdaohuo == "") {
            this.$showErrorMessage("请填写退货地址");
            this.focus = 1;
            return;
          }
        });
        let obj = {
          bowOutCertBO: message, //基本信息
          bowOutBillBOList: this.purchaseData, //采购清单
          uploadIds: this.uploadIds //上传附件
        };
        saveBowOutCert(obj).then(res => {
          if (res.data.code == "0") {
            this.$message({
              showClose: true,
              message: res.data.message,
              type: "success",
              ccuiCustom: true,
              duration: 8000
            });
            this.$router.push(
              "/checkmanagement/returnsManage/salesRegister-list"
            );
            this.isTrue = false;
          }
        });
      } catch (error) {
        // this.$showErrorMessage(error);
        this.focus = error;
        console.log(error);
      }
    },
    async submitHandle(index) {
      this.commitType = index;

      if (
        !this.$route.query.bowOutCertBO ||
        !this.$route.query.bowOutBillBOList
      ) {
        if (this.allBowoutNum <= 0) {
          this.$showErrorMessage("本次退货数量合计不能为0");
          this.focus = 1;
          return;
        }
        this.publiceMessage();
      } else {
        try {
          // 编辑
          await this.getRules(this.formData.acceptancePhone);
          await this.getRules(this.formData.supplierPhone);
          await this.getRules(this.formData.bowOutPhone);
          if (this.allBowoutNum <= 0) {
            this.$showErrorMessage("本次退货数量合计不能为0");
            this.focus = 1;
            return;
          }
          let message = {
            businessType: "1",
            reportInOrgName: this.formData.reportInOrgName, //填报单位
            reportInOrgId: Number(this.formData.reportInOrgId),
            supplier: this.formData.supplier, //供应商
            //  bowOutNumber:this.formData.bowOutNumber,//退货单编号
            bowOutName: this.formData.bowOutName, //退货单名称
            acceptanceNumber: this.formData.acceptanceNumber, //验收单编号
            // bowOutPlace: this.formData.bowOutPlace, //退货地点
            acceptancePhone: this.formData.acceptancePhone, //验收人联系电话
            supplierPhone: this.formData.supplierPhone, //供应商联系电话
            contractNumber: this.formData.contractNumber, //合同编号
            contractName: this.formData.contractName, //合同名称
            compilePeople: this.formData.compilePeople, //编制人
            compileDate: Date.parse(
              this.formatDateTime(this.formData.compileDate)
            ), //编制日期
            bowOutTypeCode: this.bowOut, //退货类型
            bowOutType: this.bowOutText,
            bowOutReason: this.formData.bowOutReason, //退货原因
            // reportInOrgId: this.$route.query.bowOutCertBO.reportInOrgId, //填报单位编码
            reportOrgDeptId: this.$route.query.bowOutCertBO.reportOrgDeptId, //填报单位部门编码
            reportOrgDeptName: this.$route.query.bowOutCertBO.reportOrgDeptName, //填报单位部门
            supplierCode: this.$route.query.bowOutCertBO.supplierCode, //供应商编码
            contractId: this.$route.query.bowOutCertBO.contractId, //合同主键
            acceptanceId: this.$route.query.bowOutCertBO.acceptanceId, // 验收单主键
            submitStatus: this.commitType, //提交状态:0未提交，1已提交
            submitTime: Date.parse(new Date()), //提交时间,
            bowOutNumber: this.$route.query.bowOutCertBO.editBowOutNumber, //退货数量
            invoiceId: this.$route.query.bowOutCertBO.invoiceId,
            invoiceNumber: this.$route.query.bowOutCertBO.invoiceNumber,
            invoiceName: this.$route.query.bowOutCertBO.invoiceName,
            orderId: this.$route.query.bowOutCertBO.orderId,
            orderNumber: this.$route.query.bowOutCertBO.orderNumber,
            orderName: this.$route.query.bowOutCertBO.orderName,
            id: this.$route.query.bowOutCertBO.id, //退货单主键
            categoryName: this.categoryName,
            supplierOrgId: this.$route.query.bowOutCertBO.supplierOrgId,
            acceptor: this.formData.acceptor, //验收人
            bowOutPeople: this.formData.bowOutPeople, //退货人
            bowOutPhone: this.formData.bowOutPhone, //退货人联系电话
            bowOutDate: Date.parse(
              this.formatDateTime(this.formData.compileDate)
            ) //退货日期
          };
          if (this.bowOut == "1") {
            if (this.uploadIds.length <= 0) {
              this.$showErrorMessage("请上传附件");
              this.focus = 2;
              return;
            }
          }
          this.purchaseData.forEach(e => {
            if (e.testdaohuo == "") {
              this.$showErrorMessage("请填写退货地址");
              this.focus = 1;
              return;
            }
          });
          let obj = {
            bowOutCertBO: message, //基本信息
            bowOutBillBOList: this.purchaseData, //采购清单
            uploadIds: this.uploadIds, //上传附件
            version: this.$route.query.bowOutCertBO.version //版本
          };
          editBowOutCert(obj).then(res => {
            if (res.data.code == "0") {
              this.$message({
                showClose: true,
                message: res.data.message,
                type: "success",
                ccuiCustom: true
              });
              this.$router.push(
                "/checkManagement/returnsManage/salesRegister-list"
              );
              this.isTrue = false;
            }
          });
        } catch (error) {
          this.$showErrorMessage(error);
        }
      }
    },
    // 退货地点弹框
    returnSite(row) {
      this.$refs.listSettings.showDialogVisible = true;
      this.$refs.listSettings.getQryAreaAddress();
      this.checkId = row.id;
      this.rowSite = {
        ...row,
        city: row.cityCode,
        province: row.provinceCode,
        deliveryPoints: row.acceptancePlace,
        deliveryAreaName: row.areaName,
        deliveryAreaCode: row.areaCode
      };
      this.$nextTick(() => {
        this.$refs.listSettings.init();
      });
    },
    getSite(val) {
      let arr = [];
      if (this.selectVal.length > 0) {
        this.selectVal.forEach(element => {
          this.purchaseData.forEach(item => {
            if (element.id === item.id) {
              item.testdaohuo =
                val.deliveryAreaName +
                val.provinceName +
                (val.cityName || "") +
                (val.countyName || "") +
                (val.deliveryPoints || "");

              // 数据内加入未拼接的的值
              // 所在区域
              item.areaName = val.deliveryAreaName;
              item.areaCode = val.deliveryAreaCode;
              //所在省/国家编码
              item.provinceCode = val.province;
              //所在省名称
              item.provinceName = val.provinceName;
              //所在市/城市编码
              item.cityCode = val.city;
              //所在市/城市名称
              item.cityName = val.cityName;
              //所在县编码
              item.countyCode = val.county;
              //所在县名称
              item.countyName = val.countyName;
              //地点
              item.acceptancePlace = val.deliveryPoints;
            }
            // arr.push(item);
          });
        });
      } else {
        this.purchaseData.forEach(item => {
          if (item.id === this.checkId) {
            item.testdaohuo =
              val.deliveryAreaName +
              val.provinceName +
              (val.cityName || "") +
              (val.countyName || "") +
              (val.deliveryPoints || "");

            // 数据内加入未拼接的的值
            // 所在区域
            item.areaName = val.deliveryAreaName;
            item.areaCode = val.deliveryAreaCode;
            //所在省/国家编码
            item.provinceCode = val.province;
            //所在省名称
            item.provinceName = val.provinceName;
            //所在市/城市编码
            item.cityCode = val.city;
            //所在市/城市名称
            item.cityName = val.cityName;
            //所在县编码
            item.countyCode = val.county;
            //所在县名称
            item.countyName = val.countyName;
            //地点
            item.acceptancePlace = val.deliveryPoints;
          }
          arr.push(item);
        });
        this.checkId = "";
        this.purchaseData = arr;
      }
      this.$forceUpdate();
      this.$refs.multipleTable.clearSelection();
    },
    batchRow() {
      if (!this.selectVal.length) {
        // this.$showErrorMessage("请选择一条数据");
        // return;
        this.selectVal = this.purchaseData;
      }
      this.$refs.listSettings.showDialogVisible = true;
      this.$refs.listSettings.getQryAreaAddress();
      this.rowSite = {};
    },
    getUserInfo() {
      getUserInfo(
        addUrlPerFix(process.env.VUE_APP_BASEP_URL) + "/apis/basap/getUserInfo",
        {}
      ).then(res => {
        const { reportOptions, userInfo } = res.data.data;
        this.formData.compilePeople = userInfo.name;
        this.reportOptions = reportOptions;
        this.userOpenId = res.data.data.userInfo.openId;
        this.getReportInOrgData(res.data.data.userInfo.openId);
      });
    },
    // 填报信息
    getReportInOrgData(openId) {
      getReportInOrgData({ openIds: [openId] }).then(res => {
        this.reportInOrgObj.tableData = res.data.data[openId];
        this.reportInOrgObj.recordsTotal = res.data.data[openId].length;
        this.reportOptions = res.data.data[openId];
        this.formData.reportInOrgName =
          this.formData.reportInOrgName || this.reportOptions[0].name;
        this.formData.reportInOrgId =
          this.formData.reportInOrgId || this.reportOptions[0].orgId;
      });
    },
    changeReportInOrgId(val) {
      const tempObj = this.reportOptions.find(e => e.name === val);
      this.formData.reportInOrgId = tempObj.orgId;
      this.formData.reportInOrgName = tempObj.name;

      // 改变填报单位清空 数据
      this.formData.supplier = "";
      this.formData.bowOutNumber = "";
      this.formData.bowOutName = "";
      this.formData.acceptanceNumber = "";
      this.formData.supplierPhone = "";
      this.formData.acceptor = "";
      this.formData.acceptancePhone = "";
      this.formData.bowOutPeople = "";
      this.formData.bowOutPhone = "";
      this.formData.bowOutDate = "";
      this.formData.contractNumber = "";
      this.formData.contractName = "";
      this.formData.bowOutType = "";
      this.formData.bowOutReason = "";

      this.purchaseData = [];
      // this.$emit("updataList", this.tableData);
      this.$nextTick(() => {
        this.$refs.formData.clearValidate();
      });
    },
    must(obj) {
      if (obj.columnIndex == 7 || obj.columnIndex == 10) {
        return "must";
      }
    }
  },
  mounted() {
    this.getUserInfo();
    this.$set(this.formData, "compileDate", new Date());
    if (this.$route.query.bowOutCertBO) {
      if (this.$route.query.bowOutCertBO.bowOutTypeCode == "1") {
        this.formData.bowOutType = "质量问题";
      } else if (this.$route.query.bowOutCertBO.bowOutTypeCode == "2") {
        this.formData.bowOutType = "其他";
      }
    }
  }
};
</script>
<style lang="scss" scoped>
/deep/
  .ccui-multifunctional-table-wrapper
  .el-table
  .el-table__footer-wrapper
  tbody
  td:nth-child(1) {
  color: #4a4a4a;
}
/deep/
  .ccui-multifunctional-table-wrapper
  .el-table
  .el-table__footer-wrapper
  tbody
  td {
  font-size: 13px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 600;
  color: #0089ef;
}
.top-title {
  font-family: PingFang SC;
  font-size: 14px;
  font-weight: 600;
  display: inline-block;
  height: 23px;
  line-height: 20px;
}
// 基本信息样式
.relative {
  position: relative;
  .chereay {
    margin-left: 10px;
    position: absolute;
  }
}

.demo-ruleForm {
  width: 80%;
  margin: 20px 0 0 50px;
}

/deep/ .el-table th.must > .cell:before {
  content: "* ";
  color: #ff1818;
}
/deep/ .ccui-multifunctional-table-wrapper .el-table__header-wrapper th {
  border-right: 1px solid #edf2fc;
}
/deep/ .ccui-multifunctional-table-wrapper .el-table td {
  border-right: 1px solid #edf2fc;
}
/deep/.el-table {
  .el-table__body-wrapper {
    z-index: 2;
  }
}
/deep/.el-table__fixed-footer-wrapper tbody td {
  background: #fff;
}
::v-deep {
  .el-input input {
    padding-right: 50px;
  }
}
/deep/ .el-table__fixed-header-wrapper {
  th {
    pointer-events: auto;
  }
}

/deep/ .el-table__fixed,
/deep/.el-table__fixed-right {
  pointer-events: none;
  td {
    pointer-events: auto;
  }
}
</style>
